package com.powernode.controller;

import com.powernode.common.Constants;
import com.powernode.common.Result;
import com.powernode.domain.User;
import com.powernode.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("login")
public class LoginController {
    @Resource
    private UserService userService;

    /**
     * 登录方法
     */
    @PostMapping("login.action")
    public Result login(String username, String password,String captcha,HttpSession session) {
        //从session域中获取正确的验证码
        String trueCode = session.getAttribute(Constants.CAPTCHA_CODE_KEY).toString();
        //判断用户输入的验证码和正确验证码是否一致
        if(trueCode.equalsIgnoreCase(captcha)) {
            User user = userService.login(username,password);
            System.out.println(password);
            if(null == user){
                return new Result(-1,"用户名或密码不正确");
            }
            //把用户放到session
            user.setPassword("");
            session.setAttribute(Constants.SESSION_USER_KEY,user);
            return new Result(200,"登录成功");
        }else {
            return new Result(-1,"验证码不正确");
        }
    }
}
